package springboot.study.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springboot.study.project.common.Result;
import springboot.study.project.common.ResultEnum;
import springboot.study.project.model.User;
import springboot.study.project.service.UserService;
import springboot.study.project.util.common.ResultUtil;
import springboot.study.project.util.common.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: 用户控制层
 * @Author: liu-hao
 * @Date: 2021-03-07 10:49
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result login(String userName, String passWord, String role, String isRememberPwd, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtil.isBlank(userName) || StringUtil.isBlank(passWord)) {
            return ResultUtil.failure("用户名或密码不能为空！");
        }
        if (StringUtil.isBlank(role)) {
            return ResultUtil.failure("用户角色不能为空！");
        }

        int isLogin = userService.login(userName, passWord, role, isRememberPwd, request, response);
        if (isLogin > 0)
            return ResultUtil.success();
        else if (isLogin == -1)
            return ResultUtil.failure(ResultEnum.ERROR_100001);
        else if (isLogin == -2)
            return ResultUtil.failure(ResultEnum.ERROR_100002);
        else
            return ResultUtil.failure(ResultEnum.UNKNOWN);
    }

    @RequestMapping("/logout")
    public Result logout(String userId, HttpServletRequest request, HttpServletResponse response) {
        userService.logout(userId, request, response);
        return ResultUtil.success();
    }


    /**
     * 添加用户
     */
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        if (user == null || StringUtil.isBlank(user.getUserName()) || StringUtil.isBlank(user.getPassword())) {
            return ResultUtil.failure("填写用户信息不完整！");
        }
        boolean result = userService.addUser(user);
        if (!result) {
            return ResultUtil.failure("添加用户失败, 可能用户名存在相同！");
        }
        return ResultUtil.success();
    }

    /**
     * 删除用户
     */
    @RequestMapping("/removeUser")
    public Result removeUser(@RequestParam(name = "userId") String userId) {
        boolean result = userService.removeUser(userId);
        if (!result) {
            return ResultUtil.failure("删除用户失败！");
        }
        return ResultUtil.success();
    }

    /**
     * 修改用户
     */
    @RequestMapping("/modifyUserInfo")
    public Result modifyUserInfo(@RequestBody User user) {
        if (user == null || StringUtil.isBlank(user.getId())) {
            return ResultUtil.failure("填写用户信息不完整！");
        }
        boolean result = userService.modifyUserInfo(user);
        if (!result) {
            return ResultUtil.failure("修改用户失败！");
        }
        return ResultUtil.success();
    }

    /**
     * 根据用户ID查询用户
     */
    @RequestMapping("findUserById")
    public Result findUserById(@RequestParam(name = "userId") String userId) {
        User user = userService.findUserById(userId);
        if (user == null) {
            return ResultUtil.failure("未查到用户！");
        }
        return ResultUtil.success(user);
    }

    /**
     * 获取符合条件的分页用户
     */
    @RequestMapping("/findUser")
    public Map<String, Object> findUser(@RequestParam(name = "userName") String userName, // 用户名
                                        @RequestParam(name = "realName") String realName, // 用户真实姓名
                                        @RequestParam(name = "role") String role, // 用户角色
                                        @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                        @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> resultMap = userService.findUser(userName, realName, role, offset, pageSize);
        return resultMap;
    }
}
